I wish Apple would test their code before presenting at a conference.
I added a name state variable for the TextField.
@State private var name: String = ""
...
.sheet(item: $donutToAdd) { _ in
TextField("Name", text: $name)
.padding(10)
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.black)
)
Button("Save") {
if let donutToAdd {
donutToAdd.name = name
donutList.donuts.append(donutToAdd )
self.donutToAdd = nil
self.name = ""
}
}
Button("Cancel") { self.donutToAdd = nil }
}
code-block
And DonutList needs to be a class (else you get a mutating member on immutable value).
@Observable
class DonutList {
var donuts: [Donut]
init(donuts: [Donut]) {
self.donuts = donuts
}
}
code-block